package com.flyme.module.base.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysComplan;
import com.flyme.module.base.provider.mapper.SysComplanMapper;
import com.flyme.module.base.provider.service.SysComplanService;
import org.springframework.stereotype.Service;

/**
 * 企业计划
 */
@Service
public class SysComplanServiceImpl extends BaseServiceImpl<SysComplanMapper, SysComplan> implements SysComplanService {

    /**
     * 根据名称查询该企业是否存在改计划
     */
    public SysComplan findByName(String title, Long companyId){
        return getOne(q().eq(true,"planName", title).eq(true, "companyId", companyId),false);
    }

    /**
     * 根据用户及用户部门查询其学习计划
     */
    public PqGrid selectUserPlan(Long userId, Long companyId, String deptId, PagerInfo pagerInfo){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("companyId", companyId);
        queryWrapper.le("c.starTime", DateUtil.now());
        queryWrapper.ge("c.endTime", DateUtil.now());
        //queryWrapper.and(i -> queryWrapper.like("userIds", userId).or().like("deptIds", deptId));
        queryWrapper.last("AND (userIds like '%"+userId+"%' OR deptIds like '%"+deptId+"%')");
        return pageList(queryWrapper);
    }

    /**
     * 根据用户及用户部门查询其学习计划
     */
    public PqGrid selectUserPlan2(Long userId, Long companyId, String deptId, PagerInfo pagerInfo, Long courseId){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("companyId", companyId);
        queryWrapper.le("c.starTime", DateUtil.now());
        queryWrapper.ge("c.endTime", DateUtil.now());
        queryWrapper.like("courseIds", courseId);
        //queryWrapper.and(e -> queryWrapper.like("userIds", userId).or().like("deptIds", deptId));
        queryWrapper.last("AND (userIds like '%"+userId+"%' OR deptIds like '%"+deptId+"%')");
        PqGrid pqGrid = pageList(queryWrapper);
        return pqGrid;
    }
}
